package com.tanhua.dubbo.api.mongo;

import com.mongodb.client.result.DeleteResult;
import com.tanhua.domain.mongo.RecommendVoice;
import com.tanhua.domain.mongo.Voice;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@Service
public class PeachblossomApiImpl implements PeachblossomApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 根据用户id查询用户推荐表
     * @param userId
     * @return
     */
    @Override
    public RecommendVoice findRecommendUser(Long userId,String gender) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("gender").is(gender));
        query.with(Sort.by(Sort.Direction.DESC,"score"));
        RecommendVoice one = mongoTemplate.findOne(query, RecommendVoice.class);
        return one;

    }

    /**
     * 根据语音id查询语音用户对象
     * @param voiceid
     * @return
     */
    @Override
    public Voice findSendUSer(long voiceid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(voiceid));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        Voice one = mongoTemplate.findOne(query, Voice.class);
        return one;
    }

    /**
     * 删除推荐用户
     * @param voiceid
     */
    @Override
    public void deleteRecommendUser(long voiceid) {
        Query query=new Query();
        query.addCriteria(Criteria.where("voiceId").is(voiceid));
        mongoTemplate.remove(query, RecommendVoice.class);

    }
}
